package com.zhsource.system.controller;

import com.github.pagehelper.PageInfo;
import com.zhsource.auth.PermissionScanAnno;
import com.zhsource.exception.GlobalException;
import com.zhsource.system.domain.Course;
import com.zhsource.system.query.CourseQuery;
import com.zhsource.system.service.ICourseService;
import com.zhsource.utils.ExcelUtil;
import com.zhsource.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: XIAOYU
 * @Date: 2023/10/17 11:47
 * @Version: 1.0
 * @Description:
 */
@Api(tags = "课程表接口")
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private ICourseService courseService;

    // 1、查询所有
    @ApiOperation(value = "获取所有用户信息")
    @GetMapping
    @PermissionScanAnno(name = "查询所有", sn="course:list")
    @PreAuthorize("hasAuthority('course:list')")
    public R<List<Course>> list(){
        try {
            return R.ok("查询所有成功！", courseService.list());
        } catch (GlobalException e) {
            log.error("Course模块的查询所有发生异常，异常信息为：{}", e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.error("Course模块的查询所有发生异常，异常信息为：{}", e.getMessage());
            return R.fail("系统异常，请重试！");
        }
    }

    // 2.查询单个
    @ApiOperation(value = "根据 ID 获取用户信息")
    @PermissionScanAnno(name = "查询单个", sn="course:selectById")
    @GetMapping("/{id}")
    public R<Course> selectById(@ApiParam(value = "员工 ID", required = true) @PathVariable("id") Long id){
        try {
            return R.ok("查询单个成功！", courseService.selectById(id));
        } catch (GlobalException e) {
            log.error("Course模块的查询单个发生异常，异常信息为：{}", e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.error("Course模块的查询单个发生异常，异常信息为：{}", e.getMessage());
            return R.fail("系统异常，请重试！");
        }
    }

    // 3.分页+模糊查询
    @PostMapping("/page")
    @ApiOperation(value = "根据 ID 获取用户信息")
    @PreAuthorize("hasAuthority('course:page')")
    @PermissionScanAnno(name = "分页查询", sn="course:page")
    public R<PageInfo<Course>> page(@RequestBody CourseQuery courseQuery){
        try {
            return R.ok("分页查询成功！", courseService.page(courseQuery));
        } catch (GlobalException e) {
            log.error("Course模块的分页查询发生异常，异常信息为：{}", e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.error("Course模块的分页查询发生异常，异常信息为：{}", e.getMessage());
            return R.fail("系统异常，请重试！");
        }
    }

    // 4.新增
    @PostMapping
    public R<Void> insert(@RequestBody Course course){
        try {
            courseService.insert(course);
            return R.ok("新增成功！");
        } catch (GlobalException e) {
            log.error("Course模块的新增发生异常，异常信息为：{}", e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.error("Course模块的新增发生异常，异常信息为：{}", e.getMessage());
            return R.fail("系统异常，请重试！");
        }
    }

    // 5.修改
    @PutMapping
    public R<Void> update(@RequestBody Course course){
        try {
            courseService.update(course);
            return R.ok("修改成功！");
        } catch (GlobalException e) {
            log.error("Course模块的修改发生异常，异常信息为：{}", e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.error("Course模块的修改发生异常，异常信息为：{}", e.getMessage());
            return R.fail("系统异常，请重试！");
        }
    }

    // 6.删除
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") Long id){
        try {
            courseService.delete(id);
            return R.ok("删除单个成功！");
        } catch (GlobalException e) {
            log.error("Course模块的删除单个发生异常，异常信息为：{}", e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.error("Course模块的删除单个发生异常，异常信息为：{}", e.getMessage());
            return R.fail("系统异常，请重试！");
        }
    }

    // 7.批量删除
    @PatchMapping("/batchDel")
    public R<Void> batchDel(@RequestBody List<Long> ids){
        try {
            courseService.batchDel(ids);
            return R.ok("批量删除成功！");
        } catch (GlobalException e) {
            log.error("Course模块的批量删除发生异常，异常信息为：{}", e.getMessage());
            return R.fail(e.getMessage());
        }catch (Exception e) {
            log.error("Course模块的批量删除发生异常，异常信息为：{}", e.getMessage());
            return R.fail("系统异常，请重试！");
        }
    }

    /**
     * 导出课程表
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    @PermissionScanAnno(name = "导出课程表", sn="course:export")
    public void export(HttpServletResponse response) throws IOException {
        List<Course> getAll = courseService.getAll();
        ExcelUtil.exportExcel(getAll, "0712全栈精英班课程表", "课程表", Course.class, "Course-Sheet", response);
    }

    /**
     * @Description: 课表导入模板
     * @param response: 响应对象
     * @Return:
     **/
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Course> list = new ArrayList<>();
        ExcelUtil.exportExcel(list, "0712全栈精英班课程表", "课表", Course.class, "课表导入模板", response);
    }

    /**
     * @Description: 课表导入
     * @param file: 要导入的文件
     * @Return:
     **/
    @PostMapping("/importExcel")
    public R<Void> importExcel(@RequestParam("file") MultipartFile file) {
        courseService.importExcel(file);
        return R.ok("导入成功！");
    }
}
